package com.qf.service.impl;

import com.qf.dao.LivedPetDao;
import com.qf.dao.impl.LivedPetDaoImpl;
import com.qf.entity.LivedPet;
import com.qf.service.LivedPetService;
import com.qf.utils.PageUtils;

import java.sql.SQLException;
import java.util.List;


public class LivedPetServiceImpl implements LivedPetService {
    private LivedPetDao livedPetDao=new LivedPetDaoImpl();
    @Override
    public int add(LivedPet livedPet) {
        try {
            return livedPetDao.add(livedPet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return - 1;
    }

    @Override
    public PageUtils<LivedPet> selectByPage(String name, String type, String startTime, String endTime, String currentPageNoStr, String pageSizeStr) {
        Integer currentPageNo=1;
        //对当前页进行处理
        if (currentPageNoStr==null || "".equals(currentPageNoStr)){
            currentPageNo=1;
        }else{
            currentPageNo = Integer.parseInt(currentPageNoStr);
        }

        Integer pageSize=10;

        //对页量进行处理
        if (pageSizeStr ==null ||  "".equals(pageSizeStr)){
            pageSize =10;
        }else{
            pageSize=Integer.parseInt(pageSizeStr);
        }
        PageUtils pageUtils = null;

        try {
            //查询总记录数
            Integer totalPageCount = livedPetDao.selectLivedPetCount(name,startTime,endTime,type);
            //根据总记录数计算出总页数
            Integer totalPageSize=  totalPageCount  %  pageSize ==0 ? totalPageCount  / pageSize : totalPageCount  /  pageSize+1;
            //查询的数据
            List<LivedPet> registerList = livedPetDao.selectLivedPetList(name,startTime,endTime,type,currentPageNo,pageSize);

            pageUtils = new PageUtils();
            pageUtils.setCurrentPageNo(currentPageNo);
            pageUtils.setPageSize(pageSize);
            pageUtils.setTotalPageCount(totalPageCount);
            pageUtils.setTotalPageSize(totalPageSize);
            pageUtils.setList(registerList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  pageUtils;
    }

    @Override
    public int update(LivedPet livedPet) {
        try {
            return livedPetDao.update(livedPet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return - 1;
    }

    @Override
    public int delete(String lid) {
        try {
            return livedPetDao.delete(lid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public List<LivedPet> selectLivedPetByTime(String time) {
        try {
            return livedPetDao.selectLivedPetByTime(time);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
